'use strict';

const Controller = require('egg').Controller;

class ReleaseController extends Controller {

  async addRelease () {
    // 获取前端传来的参数
    const { ctx } = this;
    ctx.body = ctx.request.body;
    let retrunMap = {};
    let transaction;
    try {
      // 建立事务对象
      transaction = await this.ctx.model.transaction();
      // 处理:比如传给service去查询数据库
      const users = await this.ctx.service.releaseService.add(ctx.body, transaction);
      //  const permission = await this.ctx.service.permissionsService.add(ctx.body, transaction);
      //  console.log(permission);
      // 提交事务
      await transaction.commit();
      retrunMap = { code: '1', msg: '成功' };
    } catch (err) {
      console.log(err);
      // 事务回滚
      await transaction.rollback();
      retrunMap = { code: '0', msg: '失败' };
    }

    ctx.body = retrunMap;
  }

}

module.exports = ReleaseController;
